{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "31935992-2313-491b-a014-f9ff439a609b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Actual Number: 5\n",
      "Predicted: 5\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Test Kserve model inference service for MNIST digits\n",
    "Input: None,28,28,1\n",
    "Output: Number Prediction\n",
    "\"\"\"\n",
    "\n",
    "from kubernetes import client \n",
    "from kserve import KServeClient\n",
    "from kserve import constants\n",
    "from kserve import utils\n",
    "from kserve import V1beta1InferenceService\n",
    "from kserve import V1beta1InferenceServiceSpec\n",
    "from kserve import V1beta1PredictorSpec\n",
    "from kserve import V1beta1SKLearnSpec\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "print(\"Actual Number: 5\")\n",
    "x_number_five = np.array([[[[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   3,\n",
    "         18,  18,  18, 126, 136, 175,  26, 166, 255, 247, 127,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,  30,  36,  94, 154, 170,\n",
    "        253, 253, 253, 253, 253, 225, 172, 253, 242, 195,  64,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,  49, 238, 253, 253, 253, 253,\n",
    "        253, 253, 253, 253, 251,  93,  82,  82,  56,  39,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,  18, 219, 253, 253, 253, 253,\n",
    "        253, 198, 182, 247, 241,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,  80, 156, 107, 253, 253,\n",
    "        205,  11,   0,  43, 154,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,  14,   1, 154, 253,\n",
    "         90,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 139, 253,\n",
    "        190,   2,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  11, 190,\n",
    "        253,  70,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  35,\n",
    "        241, 225, 160, 108,   1,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "         81, 240, 253, 253, 119,  25,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,  45, 186, 253, 253, 150,  27,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,  16,  93, 252, 253, 187,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0, 249, 253, 249,  64,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,  46, 130, 183, 253, 253, 207,   2,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  39,\n",
    "        148, 229, 253, 253, 253, 250, 182,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  24, 114, 221,\n",
    "        253, 253, 253, 253, 201,  78,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,  23,  66, 213, 253, 253,\n",
    "        253, 253, 198,  81,   2,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,  18, 171, 219, 253, 253, 253, 253,\n",
    "        195,  80,   9,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,  55, 172, 226, 253, 253, 253, 253, 244, 133,\n",
    "         11,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0, 136, 253, 253, 253, 212, 135, 132,  16,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0],\n",
    "       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,\n",
    "          0,   0]]]])\n",
    "\n",
    "KServe = KServeClient()\n",
    "\n",
    "isvc_resp = KServe.get(\"digits-recognizer\", namespace=\"kubeflow-user-example-com\")\n",
    "isvc_url = isvc_resp['status']['address']['url']\n",
    "\n",
    "t = np.array(x_number_five)\n",
    "t = t.reshape(-1,28,28,1)\n",
    "\n",
    "inference_input = {\n",
    "  'instances': t.tolist()\n",
    "}\n",
    "\n",
    "response = requests.post(isvc_url, json=inference_input)\n",
    "r = json.loads(response.text)\n",
    "print(\"Predicted: {}\".format(np.argmax(r[\"predictions\"])))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
